import pygame
from pygame.sprite import Group

from setting import setting
from ship import Ship
from GameStats import GameStats
from Button import Button
from ScoreBoard import Scoreboard
import game_function as gf

def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()
    ai_settings = setting()
    # 设置窗口大小
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    # 设置窗口标题
    pygame.display.set_caption("Alien Invasion")    

    # 创建Play按钮
    play_button = Button(ai_settings, screen, "Play")

    # 创建飞船
    ship = Ship(ai_settings,screen)
    # 创建外星人编组
    aliens = Group()
    gf.create_fleet(ai_settings,screen,ship,aliens)
    # 创建子弹编组
    bullets = Group()
    # 创建存储游戏统计信息的实例，并创建记分牌
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    # 开始游戏主循环
    while True:
        gf.check_event(ai_settings, screen, ship, aliens,bullets,stats, play_button,sb)

        if stats.game_active == True:
            ship.update()
            gf.update_bullets(ai_settings,screen,ship,aliens,bullets,sb,stats)
            gf.update_aliens(ai_settings,ship,aliens,stats, bullets, screen,sb)

        gf.update_screen(ai_settings,screen,ship,bullets,aliens,play_button,stats,sb)
        

run_game()